package com.example.textview;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.example.textview.util.ToastUtil;
import com.example.textview.widget.CircleProgressBar;

public class ProgressActivity extends AppCompatActivity {

    Button mbtn_Start,mbtn_progress_dialog1,mbtn_progress_dialog2;
    ProgressBar mpb3;
    CircleProgressBar mcircleProgressbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_progress);

        mcircleProgressbar=findViewById(R.id.circleProgressbar);
        mcircleProgressbar.setProgress(20);
        mbtn_progress_dialog1=findViewById(R.id.btn_progress_dialog1);
        mbtn_progress_dialog2=findViewById(R.id.btn_progress_dialog2);
        mpb3=findViewById(R.id.progress_3);
        mbtn_Start=findViewById(R.id.btn_satrt);
        mbtn_Start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               handler.sendEmptyMessage(0);
                //Toast.makeText(ProgressActivity.this,"加载完成",Toast.LENGTH_SHORT).show();
            }
        });


        mbtn_progress_dialog1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ProgressDialog progressDialog=new ProgressDialog(ProgressActivity.this);
                progressDialog.setTitle("Tiltle");
                progressDialog.setMessage("正在加载");
                progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialogInterface) {
                        ToastUtil.showMsg(ProgressActivity.this,"已经取消");
                    }
                });
                //progressDialog.setCancelable(false); //是否可以被取消
                progressDialog.show();
            }
        });

        mbtn_progress_dialog2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ProgressDialog progressDialog=new ProgressDialog(ProgressActivity.this);
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog.setTitle("Tiltle");
                progressDialog.setMessage("正在加载");
               progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "取消", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialogInterface, int i) {
                       dialogInterface.cancel();
                   }
               });
                progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialogInterface) {
                        ToastUtil.showMsg(ProgressActivity.this,"已经取消");
                    }
                });
                progressDialog.setCancelable(false); //是否可以被取消
                progressDialog.show();
            }
        });
    }


    Handler handler=new Handler (){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(mpb3.getProgress()<100)
            {
                handler.postDelayed(runnable,500);
               // ToastUtil.showMsg(ProgressActivity.this,"Now:"+mpb3.getProgress());

            }
            else {
               // Toast.makeText(ProgressActivity.this,"加载完成",Toast.LENGTH_SHORT).show();
                ToastUtil.showMsg(ProgressActivity.this,"加载完成");
            }
        }
    };

    Runnable runnable=new Runnable() {
        @Override
        public void run() {
            mpb3.setProgress(mpb3.getProgress()+5);
            handler.sendEmptyMessage(0);
        }
    };
}